#include "view_details.h"
#include "display_ctrl.h"
#include "platform_assert.h"
#include "lfs_file_manager_auto.h"
#include "rtc.h"
#include "datetime.h"
#include "blecom.h"
#include "battery_ctrl.h"
#include "sport_his.h"
#include "display_ctrl.h"
#include "display_interface_240x240.h"
#include "datetime.h"
#include "heart_rate_interface.h"
#include "font/num_font.h"
#include "font_mngr.h"
#include "view_common.h"
#include "view_details.inc"
#include "app_trace.h"
#include "app_timer.h"
#define LOG(fmt, ...) app_trace_log(fmt "\r\n", ##__VA_ARGS__)
const unsigned char bmp_head[] = {
    /* 数组大小：54 颜色数：6 图片：head.bmp*/
    0x05, 0x00, 0x0a, 0x00, 0x06, 0x00, 0x00, 0x00, 0x49, 0x4a, 0xd7, 0xbd, 0x9e, 0xf7, 0x10, 0x84, 0xdf, 0xff, 0x00, 0x81, 0x01, 0x02, 0x03, 0x00, 0x04, 0x05, 0x82,
    0x01, 0x05, 0x83, 0x02, 0x05, 0x83, 0x03, 0x05, 0x83, 0x03, 0x05, 0x83, 0x02, 0x05, 0x83, 0x01, 0x05, 0x83, 0x00, 0x04, 0x05, 0x82, 0x00, 0x81, 0x01, 0x02, 0x03,
};
const unsigned char bmp_tail[] = {
    /* 数组大小：54 颜色数：6 图片：tail.bmp*/
    0x05, 0x00, 0x0a, 0x00, 0x06, 0x00, 0x9e, 0xf7, 0xd7, 0xbd, 0x49, 0x4a, 0x00, 0x00, 0xdf, 0xff, 0x10, 0x84, 0x00, 0x01, 0x02, 0x03, 0x81, 0x04, 0x82, 0x05, 0x03,
    0x04, 0x83, 0x02, 0x04, 0x83, 0x01, 0x04, 0x83, 0x00, 0x04, 0x83, 0x00, 0x04, 0x83, 0x01, 0x04, 0x83, 0x02, 0x04, 0x82, 0x05, 0x03, 0x00, 0x01, 0x02, 0x03, 0x81,
};

static void show_steps_details_percent(int y, int percent, int color)
{
    int width = 132;
    int x = 79;
    int height = 10;
    int head_width = bmp_head[0] + (bmp_head[1] << 8);
    int tail_width = bmp_tail[0] + (bmp_tail[1] << 8);
    if (y == 59)
    {
        disp_draw_user_image_gray(x, y, 0x841, bmp_head);
        disp_draw_user_image_gray(x + width - tail_width, y, 0x841, bmp_tail);
    }
    else
    {
        disp_draw_user_image_transparent(x, y, 0x841, bmp_head);
        disp_draw_user_image_transparent(x + width - tail_width, y, 0x841, bmp_tail);
    }
    disp_draw_rect_color(x + head_width, y, x + width - tail_width, y + height - 1, 0x841);
    if (percent <= 0)
    {
        percent = 0;
    }
    else if (percent > 100)
    {
        percent = 100;
    }
    width = percent * width / 100;
    if (width < 1)
    {
        return;
    }
    int x_base = 0;
    int x_tail_base = 0;
    int x_tail = 0;
    if (width <= head_width)
    {
        x_base = x + width;
        disp_draw_user_image_transparent_less_x(x, y, color, bmp_head, x_base);
    }
    else if (width <= head_width + tail_width)
    {
        disp_draw_user_image_transparent(x, y, color, bmp_head);
        x_base = x + tail_width;
        x_tail = x_base;
        x_tail_base = x + width;
        disp_draw_user_image_transparent_less_x(x_tail, y, color, bmp_tail, x_tail_base);
    }
    else
    {
        disp_draw_user_image_transparent(x, y, color, bmp_head);
        int rect_width = width - head_width - tail_width;
        disp_draw_rect_color(x + head_width, y, x + head_width + rect_width - 1, y + height - 1, color);
        disp_draw_user_image_transparent(x + head_width + rect_width, y, color, bmp_tail);
    }
}
void view_details_display(int view)
{
    if (disp_ctrl.loop_cnt == 0)
    {
        disp_ctrl.com_data.i_temp[0] = sport_get_present_step();
        disp_ctrl.com_data.i_temp[1] = sport_get_calorie();
        if (system_config_get_km_mi() != 0)
        {
            disp_ctrl.com_data.i_temp[2] = sport_get_distance() * 0.6213712;
            disp_ctrl.com_data.i_temp[3] = 0;
        }
        else
        {
            disp_ctrl.com_data.i_temp[2] = sport_get_distance();
            disp_ctrl.com_data.i_temp[3] = 1;
        }
        disp_ctrl.com_data.i_temp[4] = system_config_get_target_step();
        disp_ctrl.com_data.i_temp[5] = system_config_get_target_calorie();
        disp_ctrl.com_data.i_temp[6] = system_config_get_target_dist();

        {
            disp_ctrl.auto_refresh_time = 1000;
        }
    }
    int x = 38;
    int y = 35;
    _disp_show_flash_img(DISP_LFS_FILE_INDEX_0, x, y, FILE_IMG_A115_icon);
    if (disp_ctrl.com_data.i_temp[4] != 0)
    {
        show_steps_details_percent(59, disp_ctrl.com_data.i_temp[0] * 100 / disp_ctrl.com_data.i_temp[4], 0x1756);
    }
    if (disp_ctrl.com_data.i_temp[5] != 0)
    {
        show_steps_details_percent(121, disp_ctrl.com_data.i_temp[1] * 100 / disp_ctrl.com_data.i_temp[5], 0xea68);
    }
    if (disp_ctrl.com_data.i_temp[6] != 0)
    {
        show_steps_details_percent(182, disp_ctrl.com_data.i_temp[2] * 100 / disp_ctrl.com_data.i_temp[6], 0x6b5d);
    }

    x = 81;
    y = 33;
    x = disp_draw_user_image_num(x, y, disp_ctrl.com_data.i_temp[0], 12, num_9x22_TAB);
    disp_draw_user_image(x, y + 10, bmp_step_unit);

    x = 81;
    y = 95;
    x = disp_draw_user_image_num(x, y, disp_ctrl.com_data.i_temp[1], 12, num_9x22_TAB);
    // disp_draw_rect_color(x, y + 22 - 4 - 1, x + 4 - 1, y + 22 - 1, RGB565_COLOR_WHITE);
    // x += 6;
    // x = disp_draw_user_image_num_with_digits(x, y, disp_ctrl.com_data.i_temp[1] % 1000 / 10, 2, 12, num_9x22_TAB);
    disp_draw_user_image(x, y + 10, bmp_kcal_unit);

    x = 81;
    y = 157;
    x = disp_draw_user_image_num(x, y, disp_ctrl.com_data.i_temp[2] / 1000, 12, num_9x22_TAB);
    disp_draw_rect_color(x, y + 22 - 4 - 1, x + 4 - 1, y + 22 - 1, RGB565_COLOR_WHITE);
    x += 6;
    x = disp_draw_user_image_num_with_digits(x, y, disp_ctrl.com_data.i_temp[2] % 1000 / 10, 2, 12, num_9x22_TAB);
    if (disp_ctrl.com_data.i_temp[3])
    {
        disp_draw_user_image(x, y + 10, bmp_km_unit);
    }
    else
    {
        disp_draw_user_image(x, y + 10, bmp_mi_unit);
    }
}
view_event_result_t view_details_event(int view, event_param_t const *param)
{
    switch (param->event)
    {
#ifdef EVENT_LEVEL_DOWN
    case event_move_level_down:
#endif
#ifdef EVENT_BUTTON_FRONT
    case event_button_front_long_press:
#endif
        return view_event_result_handled;
    }
    return view_event_result_not_handled;
}
